//
//  HomeViewController.m
//  TheRoadAD
//
//  Created by baixue on 16/8/4.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "HomeViewController.h"
#import "LyScrollBanner.h"
#import "ComHeaderView.h"
#import "MorePicture.h"//美图秀秀 更多
#import "TableViewController.h"
#import "SelectTableViewController.h"//精选
#import "SearchView.h"
#import "SearchViewViewController.h"



#define HeadMenuViewHeight 50

@interface HomeViewController ()<UIScrollViewDelegate,UITableViewDelegate>

{
    TableViewController *_tableViewVC;
    UIButton *_selectButten;
    UIView *_viewV;
}
//框架最下面的scrollView;
@property(nonatomic,strong)UIScrollView * bottomScrollView;
//左右滑动的scrollView
@property(nonatomic,strong)UIScrollView * contentScrollView;
//这个是模拟头部最上面那个滑动视图
@property(nonatomic,strong)UIView * headScrollView;
//这个是中间那个菜单
@property(nonatomic,retain)UIScrollView * headMenuScrollView;
@property(nonatomic,retain)UIView * headMenuView;
//模拟数据用的
@property(nonatomic,retain)NSArray * headMenuDateArray;
//这个数组放 tableView
@property(nonatomic,retain)NSMutableArray * contableTableViewArray;

//headScrollView当前被选中的按钮标记 默认为0
@property(nonatomic,assign)NSInteger SeletedButtonNumberTag;
@property(nonatomic,assign)CGFloat currentContScrollViewOffSizeWidth;//内容视图滑动的偏移量
@property(nonatomic,assign)UIView * contentViewCurrentView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";
    [self initWithUserInterface];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.headMenuDateArray = @[@"频道",@"精选"];
}
- (void)initWithUserInterface
{
    _contableTableViewArray = [[NSMutableArray alloc]init];
    [self.view addSubview:self.bottomScrollView];
    [self.bottomScrollView addSubview:self.headScrollView];
    [self createHeadMenuScrollView];
    [self setcontentScrollView];
    [self setSearch];
}

//最底层的scrollerView
- (UIScrollView *)bottomScrollView
{
    if (!_bottomScrollView) {
        _bottomScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
        _bottomScrollView.delegate = self;
        _bottomScrollView.bounces = NO;
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
        _bottomScrollView.showsVerticalScrollIndicator = NO;
        _bottomScrollView.contentSize = CGSizeMake(_bottomScrollView.bounds.size.width , _bottomScrollView.bounds.size.height + self.headScrollView.frame.size.height);
        _bottomScrollView.alwaysBounceVertical = YES;
        _bottomScrollView.alwaysBounceHorizontal = NO;
      
    }
    return _bottomScrollView;
}

//轮播图
- (UIView *)headScrollView
{
    if (!_headScrollView) {
        _headScrollView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 200*Screen_Height/736)];
        CGRect  rect = CGRectMake(0, 0, Screen_Width, 200 * Screen_Height / 736);
        NSArray *arr = @[@"lunbo1.png",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg"];
        LyScrollBanner *ScrollBanner = [LyScrollBanner LyScrollBannerWithFrame:rect
                                                                ImageNameArray:arr];
//        ScrollBanner.delegate = self;
        [_headScrollView addSubview:ScrollBanner];
    }
    return _headScrollView;
}
//下方的scrollerView
-(void)setcontentScrollView
{
    _contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headMenuScrollView.frame), CGRectGetWidth([UIScreen mainScreen].bounds), self.bottomScrollView.bounds.size.height - self.headMenuScrollView.bounds.size.height-64-49)];
    _contentScrollView.delegate = self;
    _contentScrollView.bounces = NO;
    _contentScrollView.pagingEnabled = YES;
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    _contentScrollView.showsVerticalScrollIndicator = NO;
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * 2 , _contentScrollView.bounds.size.height);

    [_bottomScrollView addSubview:_contentScrollView];
}

- (void)setHeadMenuDateArray:(NSArray *)headMenuDateArray
{
    _headMenuDateArray = headMenuDateArray;
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * _headMenuDateArray.count , _contentScrollView.bounds.size.height);

    [self createHeadMenuScrollView];
    [self.bottomScrollView addSubview:_headMenuScrollView];
    [self setContentscrollViewContentTableView];
}

//创建headmenuScrollView
- (void)createHeadMenuScrollView
{
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headScrollView.frame), CGRectGetWidth([UIScreen mainScreen].bounds), HeadMenuViewHeight)];
    scrollView.backgroundColor = [UIColor redColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    NSInteger arrayCount = _headMenuDateArray.count;
    CGFloat  buttonWidth = 0;
    if (arrayCount <= 2){
        buttonWidth = CGRectGetWidth([UIScreen mainScreen].bounds)/arrayCount;
    }
    else if (arrayCount > 2){
        buttonWidth = CGRectGetWidth([UIScreen mainScreen].bounds)/2.0;
    }
    scrollView.contentSize = CGSizeMake(buttonWidth * arrayCount , scrollView.frame.size.height);
    for (int i = 0 ; i < _headMenuDateArray.count ; i ++) {
        NSString * string = _headMenuDateArray[i];
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 8, buttonWidth, scrollView.frame.size.height-10)];
        button.backgroundColor = [UIColor whiteColor];
        button.center = CGPointMake(buttonWidth * (CGFloat)(0.5 + i), scrollView.frame.size.height/2.0);
        [button setTitle:string forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(processHeadMenuScrollViewButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000 + i;
        [scrollView addSubview:button];
    }
    UIView *lineH = [[UIView alloc] initWithFrame:CGRectMake(Screen_Width/2, 18, 1, 15)];
    lineH.backgroundColor = [UIColor colorWithRed:0.36 green:0.87 blue:0.85 alpha:1];
    [scrollView addSubview:lineH];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, buttonWidth, 2/ 667.0 * CGRectGetHeight([UIScreen mainScreen].bounds))];
    _viewV = view;
    view.backgroundColor = [UIColor colorWithRed:0.36 green:0.87 blue:0.85 alpha:1];
    view.tag = 100;
    scrollView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    [scrollView addSubview:view];
    self.headMenuScrollView = scrollView;

}

//创建下面的tableView
- (void)setContentscrollViewContentTableView
{
    TableViewController *tableViewVC = [[TableViewController alloc]init];
    tableViewVC.itemNum = 3;
    tableViewVC.view.frame = CGRectMake(0, 0, Screen_Width, _contentScrollView.frame.size.height);
    tableViewVC.tableView.delegate = self;
    tableViewVC.tableView.tag = 100;
    tableViewVC.tableView.rowHeight = Screen_Width * 40 / 319 + 180 * Screen_Width/319 + 100 * Screen_Width/319;
    [self addChildViewController:tableViewVC];
    
    
    SelectTableViewController *tableView = [[SelectTableViewController alloc]init];
    tableView.itemNum = 3;
    tableView.view.frame = CGRectMake(Screen_Width, 100, Screen_Width, _contentScrollView.frame.size.height-100);
    tableView.tableView.delegate = self;
    tableView.tableView.tag = 101;
    tableView.tableView.rowHeight = 270 * Screen_Width/319;
    [self addChildViewController:tableView];
    [self.contableTableViewArray addObject:tableViewVC.view];
    [self.contableTableViewArray addObject:tableView.view];

    [_bottomScrollView.panGestureRecognizer requireGestureRecognizerToFail:tableView.tableView.panGestureRecognizer];
    [self contentScrollViewArrangementContent];
}

//按钮事件
- (void)processHeadMenuScrollViewButton:(UIButton *)button
{
    
    _selectButten = button;
    [UIView animateWithDuration:0.25 animations:^{
        self.contentScrollView.contentOffset = CGPointMake((button.tag-1000 )*Screen_Width, 0);
        _viewV.frame = CGRectMake((button.tag - 1000)*Screen_Width/2, 44, Screen_Width/2, 2/ 667.0 * CGRectGetHeight([UIScreen mainScreen].bounds));
    } completion:nil];
    
}
#pragma mark -- contentScrollView方法部分
//contentScrollView内容整理和更换
- (void)contentScrollViewArrangementContent
{
    NSInteger number = _headMenuDateArray.count;
    for (int i = 0; i < number; i ++) {
        UIView * view = _contableTableViewArray[i];
        view.center = CGPointMake(_contentScrollView.frame.size.width * (i + 0.5) , _contentScrollView.frame.size.height/2.0);
        
        [_contentScrollView addSubview:view];
        
        if (i == _SeletedButtonNumberTag) {
            _contentScrollView.contentOffset = CGPointMake(_contentScrollView.frame.size.width * i, 0);
            _currentContScrollViewOffSizeWidth = _contentScrollView.frame.size.width * i;
            self.contentViewCurrentView = view;
            
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _contentScrollView && scrollView.contentOffset.x != _currentContScrollViewOffSizeWidth) {
        dispatch_async(dispatch_get_main_queue(), ^{
            @synchronized(scrollView){
                
                if (scrollView.contentOffset.x >= _currentContScrollViewOffSizeWidth + _contentScrollView.frame.size.width || scrollView.contentOffset.x <= _currentContScrollViewOffSizeWidth - _contentScrollView.frame.size.width ) {
                    
                    if (scrollView.contentOffset.x >= _currentContScrollViewOffSizeWidth + _contentScrollView.frame.size.width || scrollView.contentOffset.x <= _currentContScrollViewOffSizeWidth - _contentScrollView.frame.size.width ) {
                            if (scrollView.contentOffset.x > _currentContScrollViewOffSizeWidth && _selectButten.tag-1000 < _headMenuDateArray.count - 1) {
                                _selectButten = (UIButton *)[_headMenuScrollView viewWithTag:_selectButten.tag + 1];
                            }
                            else if (scrollView.contentOffset.x < _currentContScrollViewOffSizeWidth && _selectButten.tag > 1000){
                                _selectButten = (UIButton *)[_headMenuScrollView viewWithTag:_selectButten.tag - 1];
                            }
                        }
                        if (_selectButten.tag - 1000 > 0 && _selectButten.tag - 1000 < _headMenuDateArray.count - 1) {
                            [_contentScrollView setContentOffset:CGPointMake(_contentScrollView.frame.size.width , 0)];
                            _currentContScrollViewOffSizeWidth = _contentScrollView.frame.size.width;
                            
                        }else{
                            
                            _currentContScrollViewOffSizeWidth = roundf((scrollView.contentOffset.x/_contentScrollView.frame.size.width)) * _contentScrollView.frame.size.width;
                        }
                    _selectButten.selected = YES;
                    _SeletedButtonNumberTag = _selectButten.tag - 1000;
                    [self contentScrollViewArrangementContent];
                }
            }
        });
    }else if (scrollView == self.contentViewCurrentView) {
        //悬浮框
        if (_bottomScrollView.contentOffset.y < self.headScrollView.bounds.size.height || scrollView.contentOffset.y < 0) {
            scrollView.delegate = nil;
            _bottomScrollView.delegate = nil;
            CGPoint contentOffsetPoint = _bottomScrollView.contentOffset;
            contentOffsetPoint.y = contentOffsetPoint.y + (scrollView.contentOffset.y < 0 ? scrollView.contentOffset.y / 2.0: scrollView.contentOffset.y);
            scrollView.contentOffset = CGPointMake(0, 0);
            _bottomScrollView.contentOffset = contentOffsetPoint;
            scrollView.delegate = self;
            _bottomScrollView.delegate = self;
        }
        else if (_bottomScrollView.contentOffset.y > self.headScrollView.bounds.size.height){
            _bottomScrollView.delegate = nil;
            _bottomScrollView.contentOffset = CGPointMake(_bottomScrollView.contentOffset.x, self.headScrollView.bounds.size.height);
            _bottomScrollView.delegate = self;
            
        }
        if (_bottomScrollView.contentOffset.y < 0) {
            _bottomScrollView.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
           
        }
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _contentScrollView) {
        _bottomScrollView.delegate = nil;
        NSInteger index = _contentScrollView.contentOffset.x/Screen_Width;
        [UIView animateWithDuration:0.25 animations:^{
            _viewV.frame = CGRectMake(Screen_Width/2 * index, 44, Screen_Width/2, 2);
            _contentScrollView.contentOffset = CGPointMake(_contentScrollView.frame.size.width * index, 0);
            _currentContScrollViewOffSizeWidth = _contentScrollView.frame.size.width * index;
        }];
        
    }
}



-(void)setSearch{
    SearchView *view = [[SearchView alloc] initWithFrame:CGRectMake(Screen_Width, 0, Screen_Width, 50)];
    [_contentScrollView addSubview:view];
    
    //页面跳转
    view.SEARCH = ^(){
       SearchViewViewController *s = [[SearchViewViewController alloc] init];
        [self.navigationController pushViewController:s animated:YES];
    };
}


@end
